package com.huarui.department.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.ewaf.framework.model.ListModel;
import org.ewaf.framework.service.BaseService;


import com.huarui.department.dao.IDepartmentDao;
import com.huarui.department.model.Department;
import com.huarui.department.model.DepartmentListModel;


public class DepartmentService extends BaseService{
	/**
	 * 得到dao
	 * 
	 * @return
	 */
	private IDepartmentDao getDao() {

		return (IDepartmentDao) dao;
	}
	
	/**
	 * 
	 * 获得所有部门
	 * @return
	 */
	public List<Department> getAll(){
		this.getDao().getAll();
		return null;
		
	}
	//Object...params
	@SuppressWarnings("unchecked")
	public ListModel getDepartmentsByCondition(DepartmentListModel lm,Integer companyId){
		try {
			Map map = new HashMap();
			map.put("companyId", companyId);
			map.put("isDelete", lm.getIsDelete());
			map.put("orderBy", lm.getOrderBy());
			map.put("start", lm.getStart());
			map.put("pageSize", lm.getPageSize());
			map.put("desc", lm.isDesc());
		int total=this.getDao().getByConditionsForCount(map);
		lm.setTotal(total);	
		List result=this.getDao().getDepartmentsByConditions(map);
		lm.setResult(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lm;
	}
	@SuppressWarnings("unchecked")
	public ListModel<Department> getDepartmentsBycompanyId(ListModel<Department>lm,Integer companyId,String keyword){

		Map map = new HashMap<String, String>();

		@SuppressWarnings("unused")
		String regNubmer = "^[0-9A-Za-z]+$";
		String regABC = "^[A-Za-z]+$";
		if (StringUtils.isNotBlank(keyword)) {

			if (keyword.matches(regABC)) {

				map.put("pyCode", keyword);

			} else {

				map.put("name", keyword);
			}

		}
		map.put("start", lm.getStart());
		map.put("pageSize", lm.getPageSize());
		map.put("orderBy","id");
		map.put("isDelete", 0);
		map.put("desc", false);
		map.put("companyId", companyId);

		lm.setTotal(this.getDao().getByConditionsForCount(map));
		
		List result=this.getDao().getDepartmentsByConditions(map);
		
		lm.setResult(result);
		
		return lm;
	}
	
	public Department getByEmployeeId(Integer employeeId){
		
		
		return getDao().getByEmployeeId(employeeId);
		
	}
	
	@SuppressWarnings("unchecked")
	public Department getByName(Map map){
		
		
		return getDao().getByName(map);
		
	}
	
	public Department getByisDefault(Integer companyId){
		
		return this.getDao().getByisDefault(companyId);
		
	}
	
}
